using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dune.Business.Domain.Application;
using Dune.Business.Domain.Forum;
using Edge.Business.Domain.Application;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Web.Controls.Grid;
using Edge.Common.Web.DataSourceAdapters;
using Dune.Web.AppCode;
using SortDirection=Edge.Common.SortDirection;

namespace Dune.Web
{
    public partial class Posts : BaseDunePage
    {
        [QueryParameter("id", IsRequired = true)]
        public int themeID;

        private FilteredDataSourceFromSession<IPostManager, Post, PostFilter> dataSource;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            Theme currentTheme = null;
            if (themeID != 0)
            {
                currentTheme = Locator.GetService<IThemeManager>().Load(themeID);
                lblHeaderTheme.Text = currentTheme.Description;
            }
            gridPosts.DataBind();
           // rprContent.DataSource = Locator.GetService<IPostManager>().GetPosts(themeID);
           // rprContent.DataBind();
        }
        protected void btAdd_Click(object sender, EventArgs e)
        {
            Post post =
                new Post(tbNewPost.Text, Locator.GetService<IThemeManager>().Load(themeID),
                         Locator.GetService<IContextObjects>().Get<PersonHolder>().Object, DateTime.Now);
            Locator.GetService<IPostManager>().Save(post);
            Response.Redirect(Page.Request.Url.AbsoluteUri);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            WebParameterAttribute.SetValues(this, Request);
            dataSource = new FilteredDataSourceFromSession<IPostManager, Post, PostFilter>(gridPosts, GetTemplateFilter());
            GridHandlers.Setup(gridPosts, "CreationTime", SortDirection.Ascending);

        }

        protected PostFilter GetTemplateFilter()
        {
            PostFilter template = new PostFilter();
            //template.Person = Locator.GetService<IContextObjects>().Get<PersonHolder>().Object;
            template.Theme = Locator.GetService<IThemeManager>().Load(themeID); 
            template.Keyword = "";
            return template;
        }

        protected void gridPosts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
//            if (e.Row.RowType == DataControlRowType.DataRow)
//            {
//                Person item = (Person)e.Row.DataItem;
//                HyperLink hlEdit = (HyperLink)e.Row.FindControl("hlEdit");
//                hlEdit.NavigateUrl = "PersonDetails.aspx?action=edit&id=" + item.Id.ToString();
//                HyperLink hlDelete = (HyperLink)e.Row.FindControl("hlDelete");
//                hlDelete.NavigateUrl = "PersonDetails.aspx?action=delete&id=" + item.Id.ToString();
//            }
        }
    }
}
